package com.example.chatchatbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.chatchatbackend.entity.dto.Notice;
import com.example.chatchatbackend.entity.vo.request.NoticeVO;
import com.example.chatchatbackend.mapper.NoticeMapper;
import com.example.chatchatbackend.service.NoticeService;
import com.example.chatchatbackend.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    private static final LocalDateTime currentTime  = TimeUtil.Time_Now.getCurrentTime();

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public List<NoticeVO> getAllNotice() {
        List<Notice> noticeList = this.query().list();
        List<NoticeVO> noticeVOList = new ArrayList<>();
        for(Notice notice : noticeList){
            noticeVOList.add(new NoticeVO(notice.getId(),notice.getTitle(),notice.getContent(),notice.getCreatedTime(),notice.getUpdatedTime(),notice.getEndTime(),notice.getCreatorId(),notice.getStatus()));
        }
        return noticeVOList;
    }

    @Override
    public NoticeVO insertNotice(NoticeVO noticeVO) {
        Notice comment = new Notice(null,noticeVO.getTitle(),noticeVO.getContent(),this.currentTime,this.currentTime,noticeVO.getEndTime(),noticeVO.getCreatorId(),noticeVO.getStatus());
        if(this.save(comment))
            return noticeVO;
        return null;
    }

    @Override
    public List<NoticeVO> insertNoticeList(List<NoticeVO> noticeVOList) {

        List<Notice> noticeList = new ArrayList<>();

        for (NoticeVO noticeVO : noticeVOList) {
            noticeList.add(new Notice(null,noticeVO.getTitle(),noticeVO.getContent(),this.currentTime,this.currentTime,noticeVO.getEndTime(),noticeVO.getCreatorId(),noticeVO.getStatus()));
        }
        noticeMapper.insertList(noticeList);
        return noticeVOList;
    }


    @Override
    public IPage<Notice> pageThree(Integer pageNum, Integer pageSize) {
        Page<Notice> page = new Page<>(pageNum, pageSize);
        IPage<Notice> noticeIPage = noticeMapper.selectPage(page, null);
        return noticeIPage;
    }


    @Override
    public NoticeVO updateNotice(NoticeVO noticeVO) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeVO, notice);
        notice.setUpdatedTime(this.currentTime);
        notice.setEndTime(this.currentTime);
        noticeMapper.updateNotice(notice);
        return noticeVO;
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        noticeMapper.deleteBatchIds(ids);
    }

    public List<NoticeVO> findByTitle(String title) {

        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(StringUtils.hasText(title), Notice::getContent, title);
        List<Notice> noticeList = noticeMapper.selectList(wrapper);

        List<NoticeVO> noticeVOList = new ArrayList<>();

        for (Notice notice : noticeList) {
            NoticeVO noticeVO = new NoticeVO();
            BeanUtils.copyProperties(notice, noticeVO);
            noticeVOList.add(noticeVO);  // 添加到结果列表
        }
        return noticeVOList;
    }
}
